import logging
from io import BytesIO
from selenium import webdriver
import pytesseract
from PIL import Image

def get_code(img):
    pytesseract.pytesseract.tesseract_cmd=r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    image=Image.open(img)
    code=pytesseract.image_to_string(image,config='--psm 7 -c tessedit_char_whitelist=0123456789').strip()

    return code

def init_driver(browser="edge", headless=False):
    if browser.lower() == "edge":

        options = webdriver.EdgeOptions()
        if headless:
            options.add_argument("--headless")
        return webdriver.Edge(options=options)

    elif browser.lower() == "chrome":
        options = webdriver.ChromeOptions()
        if headless:
            options.add_argument("--headless")
        return webdriver.Chrome(options=options)
    else:
        print('未适配')


def get_logger(name="test_logger"):
    logger = logging.getLogger(name)
    if not logger.handlers:
        fmt=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        # 1. 文件输出
        file_hd = logging.FileHandler(f'WMS_.log', encoding='utf-8')
        file_hd.setFormatter(fmt)
        logger.addHandler(file_hd)

        # 2. 控制台输出
        console_hd = logging.StreamHandler()  # 等价于 StreamHandler(sys.stdout)
        console_hd.setFormatter(fmt)
        logger.addHandler(console_hd)

        logger.setLevel(logging.INFO)
    return logger